﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Repository;

namespace Lead2012.Controllers
{
    public class EmailTemplateController : BaseController
    {
        // Create an instance of repository
        EmailTemplateManagementRepository amRepository = new EmailTemplateManagementRepository();

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            EmailTemplateViewModel model = new EmailTemplateViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");
            amRepository.GetModel(model, string.Empty, string.Empty, string.Empty, 1, 10);

            return View(model);
        }

        [HttpPost]
        public ActionResult Filters(string keywords, string sortBy, string sortOrder, int pageIndex, int pageSize)
        {
            EmailTemplateViewModel model = new EmailTemplateViewModel();

            // Setup model
            if (pageSize == 0)
                pageSize = 10;
            if (pageIndex == 0)
                pageIndex = 1;

            amRepository.GetModel(model, keywords, sortBy, sortOrder, pageIndex, pageSize);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            EmailTemplateRepository repository = new EmailTemplateRepository();
            EmailTemplateViewModel model = new EmailTemplateViewModel();

            // Delete action
            repository.Delete(id);

            // Setup model
            amRepository.GetModel(model, string.Empty, string.Empty, string.Empty, 1, 10);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Add()
        {
            EmailTemplateViewModel model = new EmailTemplateViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");
            amRepository.GetModel(model, 0);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(FormCollection form)
        {
            EmailTemplateRepository repository = new EmailTemplateRepository();
            EmailTemplate model = new EmailTemplate();

            model.Subject = form["Subject"].ToString();
            model.FromEmail = form["FromEmail"].ToString();
            model.ToEmail = string.Empty;
            model.Body = form["Body"].ToString();
            model.CcEmail = form["CcEmail"].ToString();
            model.BccEmail = form["BccEmail"].ToString();
            model.IsBodyHtml = (form["IsBodyHtml"] != null && form["IsBodyHtml"].ToString() == "on") ? true : false;
            repository.Create(model);
            return Redirect("/EmailTemplate");
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            EmailTemplateViewModel model = new EmailTemplateViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");
            amRepository.GetModel(model, id);
            ViewBag.TemplateId = id;

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection form)
        {
            EmailTemplateRepository repository = new EmailTemplateRepository();
            EmailTemplate model = new EmailTemplate();

             model = repository.GetSingle(id);
             model.Subject = form["Subject"].ToString();
             model.FromEmail = form["FromEmail"].ToString();
             model.ToEmail = string.Empty;
             model.Body = form["Body"].ToString();
             model.CcEmail = form["CcEmail"].ToString();
             model.BccEmail = form["BccEmail"].ToString();
             model.IsBodyHtml = (form["IsBodyHtml"] != null && form["IsBodyHtml"].ToString() == "on") ? true : false;

            repository.Update(model);

            return Redirect("/EmailTemplate");
        }
    }
}
